// 收藏题目页面逻辑
Page({
  data: {
    totalFavorites: 0,
    completedCount: 0,
    uncompletedCount: 0,
    currentCategory: 'all',
    favorites: [],
    filteredFavorites: [],
    loading: false,
    hasMore: true,
    page: 1,
    pageSize: 10
  },

  onLoad: function (options) {
    this.loadFavorites();
    this.updateStatistics();
  },

  onShow: function () {
    // 页面显示时刷新数据
    this.loadFavorites();
  },

  // 加载收藏数据
  loadFavorites: function () {
    const favorites = wx.getStorageSync('favorites') || [];
    this.setData({
      favorites: favorites,
      filteredFavorites: this.filterFavorites(favorites, this.data.currentCategory)
    });
    this.updateStatistics();
  },

  // 更新统计数据
  updateStatistics: function () {
    const favorites = this.data.favorites;
    const totalFavorites = favorites.length;
    const completedCount = favorites.filter(f => f.completed).length;
    const uncompletedCount = totalFavorites - completedCount;

    this.setData({
      totalFavorites: totalFavorites,
      completedCount: completedCount,
      uncompletedCount: uncompletedCount
    });
  },

  // 筛选收藏
  filterFavorites: function (favorites, category) {
    if (category === 'all') {
      return favorites;
    }
    return favorites.filter(f => f.category === category);
  },

  // 设置分类筛选
  setCategory: function (e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      currentCategory: category,
      filteredFavorites: this.filterFavorites(this.data.favorites, category)
    });
  },

  // 开始练习
  startPractice: function () {
    const favorites = this.data.filteredFavorites;
    if (favorites.length === 0) {
      wx.showToast({
        title: '暂无收藏题目',
        icon: 'none'
      });
      return;
    }

    wx.navigateTo({
      url: '/pages/practice/practice?type=favorites'
    });
  },

  // 清空收藏
  clearFavorites: function () {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有收藏吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('favorites');
          this.setData({
            favorites: [],
            filteredFavorites: []
          });
          this.updateStatistics();
          
          wx.showToast({
            title: '已清空收藏',
            icon: 'success'
          });
        }
      }
    });
  },

  // 跳转到题目详情
  goToQuestion: function (e) {
    const id = e.currentTarget.dataset.id;
    
    // 查找题目的分类和专题信息
    const { questionCategories } = require('../../utils/questions.js');
    let categoryId = '';
    let topicId = '';
    
    for (const category of questionCategories) {
      for (const topic of category.topics) {
        for (const question of topic.questions) {
          if (question.id == id) {
            categoryId = category.id;
            topicId = topic.id;
            break;
          }
        }
        if (categoryId) break;
      }
      if (categoryId) break;
    }
    
    if (categoryId && topicId) {
      wx.navigateTo({
        url: `/pages/detail/detail?categoryId=${categoryId}&topicId=${topicId}&questionId=${id}`
      });
    } else {
      wx.showToast({
        title: '题目信息获取失败',
        icon: 'error'
      });
    }
  },

  // 取消收藏
  removeFromFavorites: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认取消收藏',
      content: '确定要取消收藏这道题吗？',
      success: (res) => {
        if (res.confirm) {
          const favorites = this.data.favorites.filter(f => f.id !== id);
          
          wx.setStorageSync('favorites', favorites);
          this.setData({
            favorites: favorites,
            filteredFavorites: this.filterFavorites(favorites, this.data.currentCategory)
          });
          this.updateStatistics();
          
          wx.showToast({
            title: '已取消收藏',
            icon: 'success'
          });
        }
      }
    });
  },

  // 立即练习
  practiceQuestion: function (e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/practice/practice?questionId=${id}`
    });
  },

  // 跳转到练习页面
  goToPractice: function () {
    wx.switchTab({
      url: '/pages/practice/practice'
    });
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    this.loadFavorites();
    wx.stopPullDownRefresh();
  },

  // 分享功能
  onShareAppMessage: function () {
    return {
      title: '我的收藏 - 编程题库',
      path: '/pages/favorites/favorites'
    };
  }
}); 